import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../app/config.dart';

extension AppTextField on TextField {
  static Widget titleInput({
    required String title,
    TextEditingController? controller,
    FocusNode? focusNode,
    bool readOnly = false,
    String? hintText,
    List<TextInputFormatter>? inputFormatters,
    TextInputType? keyboardType,
    bool obscureText = false,
    TextInputAction? textInputAction,
    BoxBorder? border,
    void Function()? onEditingComplete,
    void Function(String)? onSubmitted,
    List<Widget>? suffixIcons,
  }) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12),
        color: Colors.white,
        border:border ?? Border.all(width: 1, color: Colors.black),
      ),
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: 12,
              color: Colors.black,
            ),
          ),
          TextField(
            focusNode: focusNode,
            readOnly: readOnly,
            controller: controller,
            inputFormatters: inputFormatters,
            keyboardType: keyboardType,
            obscureText: obscureText,
            style: appGlobalConfig.widgetConfig.textFieldTextStyle,
            obscuringCharacter: '*',
            onEditingComplete: onEditingComplete,
            onSubmitted: onSubmitted,
            textInputAction: textInputAction,
            decoration: InputDecoration(
              isDense: true,
              hintText: hintText,
              hintStyle: appGlobalConfig.widgetConfig.textFieldHintStyle,
              border: InputBorder.none,
              suffixIcon: suffixIcons != null && suffixIcons.isNotEmpty
                  ? Row(
                      mainAxisSize: MainAxisSize.min,
                      children: suffixIcons,
                    )
                  : null,
              suffixIconConstraints: BoxConstraints.tightFor(height: 24),
            ),
          ),
        ],
      ),
    );
  }

  static Widget input({
    TextEditingController? controller,
    FocusNode? focusNode,
    double? height = 56,
    BoxBorder? border,
    EdgeInsets contentPadding = const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
    bool readOnly = false,
    Color bgColor = Colors.white, // 使用静态颜色作为默认值
    String? hintText,
    List<TextInputFormatter>? inputFormatters,
    TextInputType? keyboardType,
    bool obscureText = false,
    TextInputAction? textInputAction,
    void Function()? onEditingComplete,
    void Function(String)? onSubmitted,
    Widget? suffixIcon,
    BoxConstraints? suffixIconConstraints,
  }) {
    return Container(
      width: double.infinity,
      height: height,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12),
        color:  bgColor,
        border: border,
      ),
      padding: contentPadding,
      alignment: Alignment.center,
      child: TextField(
        textAlignVertical: TextAlignVertical.center,
        focusNode: focusNode,
        readOnly: readOnly,
        controller: controller,
        inputFormatters: inputFormatters,
        keyboardType: keyboardType,
        obscureText: obscureText,
        style: appGlobalConfig.widgetConfig.textFieldTextStyle,
        obscuringCharacter: '*',
        onEditingComplete: onEditingComplete,
        onSubmitted: onSubmitted,
        textInputAction: textInputAction,
        decoration: InputDecoration(
          isDense: true,
          hintText: hintText,
          hintStyle: appGlobalConfig.widgetConfig.textFieldHintStyle,
          border: InputBorder.none,
          contentPadding: EdgeInsets.zero,
          suffixIcon: suffixIcon,
          suffixIconConstraints: suffixIconConstraints,
        ),
      ),
    );
  }

  static Widget prefixInput({
    TextEditingController? controller,
    FocusNode? focusNode,
    double? height = 68, // 调整默认高度
    BoxBorder? border,
    EdgeInsets contentPadding = const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
    bool readOnly = false,
    Color bgColor = Colors.white, // 使用静态颜色作为默认值
    String? hintText,
    List<TextInputFormatter>? inputFormatters,
    TextInputType? keyboardType,
    bool obscureText = false,
    TextInputAction? textInputAction,
    void Function()? onEditingComplete,
    void Function(String)? onSubmitted,
    Widget? prefixIcon, // 添加前置图标参数
    Widget? suffixIcon,
    BoxConstraints? suffixIconConstraints,
  }) {
    return Container(
      width: double.infinity,
      height: height,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(8), // 调整圆角
        color: bgColor,
        border: border,
      ),
      padding: contentPadding,
      // alignment: Alignment.st,
      child: Row(  // 使用Row来布局前置图标和输入框
        children: [
          if (prefixIcon != null)
            Padding(
              padding: EdgeInsets.only(right: 0),
              child: prefixIcon,
            ),
          Expanded(
            child: TextField(
              textAlignVertical: TextAlignVertical.center,
              focusNode: focusNode,
              readOnly: readOnly,
              controller: controller,
              inputFormatters: inputFormatters,
              keyboardType: keyboardType,
              obscureText: obscureText,
              style: appGlobalConfig.widgetConfig.textFieldTextStyle,
              obscuringCharacter: '•', // 修改密码遮罩字符
              onEditingComplete: onEditingComplete,
              onSubmitted: onSubmitted,
              textInputAction: textInputAction,
              decoration: InputDecoration(
                isDense: true,
                hintText: hintText,
                hintStyle: appGlobalConfig.widgetConfig.textFieldHintStyle,
                border: InputBorder.none,
                contentPadding: EdgeInsets.zero,
                suffixIcon: suffixIcon,
                suffixIconConstraints: suffixIconConstraints,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
